#include "searcher.hpp"
#include "cpp-httplib/httplib.h"
#include "Log.hpp"
#include <memory>

using namespace LogModule;

const std::string root_path = "./wwwroot";
const std::string input = "data/raw_html/raw.txt";

int main()
{
    ns_seracher::Searcher search;
    search.InitSeracher(input);

    httplib::Server svr;
    
    svr.set_base_dir(root_path.c_str()); 
    svr.Get("/s", [&search](const httplib::Request &req, httplib::Response &rsp) -> void {
        if(!req.has_param("word")) {
            rsp.set_content("必须要有搜索关键字!", "text/plain; charset=utf-8");
            return ;
        }
        std::string word = req.get_param_value("word");
        LOG(LogLevel::INFO) << "用户在搜索: " << word;
        std::string json_string;
        search.Search(word, &json_string);
        rsp.set_content(json_string, "application/json");
    });

    LOG(LogLevel::INFO) << "服务器启动成功... ";
    svr.listen("0.0.0.0", 80);

    return 0;
}